<?php
if (isset($_POST['OBCJ'])) {
    $panel['name'] = $_POST['OBCJ'];
    $SearchParam = $_POST['SearchParam'];
    $SearchValue = $_POST['SearchValue'];
} else {
    $panel['name'] = $_GET['OBJ'];
    $SearchParam = $_GET['SearchParam'];
    $SearchValue = $_GET['SearchValue'];
}
$Qr_Operator = '=';
$Joins = '';

if ($SearchParam == 'FraReferenciaAg') {
    $__Val = explode('-', $SearchValue);
    $SearchValue = $__Val[1] . ' AND FraOrigen0 = ' . $__Val[0];
}
if ($SearchParam == 'CliNombre' || $SearchParam == 'BnfNombre') {
    $Qr_Operator = 'LIKE';
}
if ($SearchParam == 'dtd_Numero') {
    $Joins = 'INNER JOIN hd_documentos ON dtd_Cliente = CliNumero';
}
if ($SearchParam == 'uniBeneficiario') {
    $Joins = 'INNER JOIN unionclientesbeneficiarios ON uniCliente = CliNumero';
}
if ($SearchParam == 'uniCliente') {
    $Joins = 'INNER JOIN unionclientesbeneficiarios ON uniBeneficiario = BnfNumero';
}
if ($SearchParam == 'BnfNombre' && $panel['name'] == 'CL') {
    $Joins = 'INNER JOIN unionclientesbeneficiarios ON uniCliente = CliNumero
				INNER JOIN beneficiarios ON BnfNumero = uniBeneficiario';
}

if ($Qr_Operator == 'LIKE') {
    $SearchValue = "'%" . $SearchValue . "%'";
}

if ($panel['name'] == 'OR') {
    $OB_SELEC = 'ORDEM';
} elseif ($panel['name'] == 'CL') {
    $OB_SELEC = 'CLIENTE';
} elseif ($panel['name'] == 'RC') {
    $OB_SELEC = 'BENEFICIARIO';
} elseif ($panel['name'] == 'AC') {
    $OB_SELEC = 'CONTA';
} elseif ($panel['name'] == 'DP') {
    $OB_SELEC = 'DEPOSITO';
} elseif ($panel['name'] == 'EX') {
    $OB_SELEC = 'EXCHANGE';
} elseif ($panel['name'] == 'DC') {
    $OB_SELEC = 'DOCUMENTOS';
}
if (!isset($_GET['Lim']) || $_GET['Lim'] == 0) {
    $qrLimit = '';
} else {
    $qrLimit = ' AND FraFechaIngreso > DATE_SUB(CURRENT_DATE,INTERVAL ' . $_GET['Lim'] . ' MONTH)';
}

if ($panel['name'] == 'OR') {
    $OBCTQRY = "SELECT FraID AS O_ID, CONCAT(FraOrigen0,'-',FraReferenciaAg) AS COL1, FraFechaIngreso AS COL4, BnfNombre AS COL7, FraEnviado COL2, FraARecibir AS COL3, FraCargo AS COL5, sg_Abreviatura AS COL6
  			FROM facturas
				LEFT JOIN beneficiarios ON BnfNumero = FraBnfNumero
				INNER JOIN hd_statusfacturas ON sg_ID = FraStatus
			WHERE {$SearchParam} {$Qr_Operator} {$SearchValue}{$qrLimit}";
} elseif ($panel['name'] == 'CL') {

    $OBCTQRY = "SELECT CliNumero AS O_ID, CliNombre AS COL1, CliTelefono AS COL4, CliTelefono2 COL2, CliDomicilio AS COL3, ClPais AS COL5, ClObs AS COL6
  			FROM clientes
			 {$Joins}
			WHERE {$SearchParam} {$Qr_Operator} {$SearchValue}";
} elseif ($panel['name'] == 'DC') {
    $OBCTQRY = "SELECT dtd_ID AS O_ID, tdc_Abreviatura AS COL5, dtd_Numero AS COL1, dtd_Fecha COL2, dtd_Emisor AS COL3, name AS COL4, dtd_Status AS COL6
  			FROM hd_documentos
				INNER JOIN hd_documentostipos ON dtd_Tipo = tdc_Id
				INNER JOIN systemoffice.main_users ON ref = dtd_Usuario
			WHERE {$SearchParam} {$Qr_Operator} {$SearchValue}";
} elseif ($panel['name'] == 'RC') {
    $OBCTQRY = "SELECT BnfNumero AS O_ID, BnfNombre AS COL1, BnfTelefono AS COL4, BnfTelefono2 COL2, BnfDireccion AS COL3, pai_NombreESP AS COL5, BnfCedula AS COL6
  			FROM beneficiarios
			LEFT OUTER JOIN hd_paises ON pai_CodElite = BnfPais
			 {$Joins}
			WHERE {$SearchParam} {$Qr_Operator} {$SearchValue}";
} elseif ($panel['name'] == 'AC') {
    $OBCTQRY = "SELECT cb_Id AS O_ID, cb_Cuenta AS COL1, bp_Name AS COL4, cb_Sucursal AS COL2, TYPE_NAME AS COL3, name AS COL5, cb_Fecha AS COL6
  			FROM beneficiarioscuentas
				LEFT OUTER JOIN hd_bancos ON bp_Id = cb_Banco
				LEFT OUTER JOIN ciudades ON CiuNumero = cb_CiudadSucursal
				LEFT OUTER JOIN hf_actypes ON hf_actypes.REF = cb_TipoCuenta
				LEFT OUTER JOIN systemoffice.main_users ON main_users.ref = cb_Usuario
			WHERE {$SearchParam} {$Qr_Operator} {$SearchValue}";
} elseif ($panel['name'] == 'EX') {
    $OBCTQRY = "SELECT CliNumero AS O_ID, CliNombre AS COL1, CliTelefono AS COL4, CliTelefono2 COL2, CliDomicilio AS COL3, ClPais AS COL5, ClObs AS COL6
  			FROM exchange_operations
			WHERE {$SearchParam} {$Qr_Operator} {$SearchValue}";
} elseif ($panel['name'] == 'DP') {
    $OBCTQRY = "SELECT CliNumero AS O_ID, CliNombre AS COL1, CliTelefono AS COL4, CliTelefono2 COL2, CliDomicilio AS COL3, ClPais AS COL5, ClObs AS COL6
  			FROM exchange_operations
			WHERE {$SearchParam} {$Qr_Operator} {$SearchValue}";
}

$DBGET = $GLOBALS['MYT']->db_array($OBCTQRY);
$CARD = $DBGET[0];
$SUM = 0;
?>

<div id="resultsList"></div>
<script charset="utf-8" type="text/javascript">
    disableSelection(document.getElementById("resultsList"));
    Ext.onReady(function(){

<?php if ($CARD > 0 || isset($_GET['Lim'])) { ?>

if(Ext.getCmp('src-window')) {
    Ext.getCmp('src-window').close();
}
var myData = [
<?php
for ($i = 1; $i <= $CARD; $i++) {
    $SUM = $SUM + $DBGET[$i]['COL2'];
    if ($panel['name'] == 'OR') {
        echo "[{$i},{$DBGET[$i]['O_ID']},\"{$DBGET[$i]['COL1']}\",\"" . number_format($DBGET[$i]['COL2'], 2, ',', '.') . "\",\"" . number_format($DBGET[$i]['COL3'], 2, ',', '.') . "\",\"{$DBGET[$i]['COL7']}\",\"{$DBGET[$i]['COL4']}\",\"" . number_format($DBGET[$i]['COL5'], 2, ',', '.') . "\",\"{$DBGET[$i]['COL6']}\"]";
    } else {
        echo "[{$i},{$DBGET[$i]['O_ID']},\"{$DBGET[$i]['COL1']}\",\"{$DBGET[$i]['COL2']}\",\"{$DBGET[$i]['COL3']}\",\"{$DBGET[$i]['COL4']}\",\"{$DBGET[$i]['COL5']}\",\"" . str_replace('"', '', $DBGET[$i]['COL6']) . "\"]";
    }
    if ($i < $CARD) {
        echo ',';
    }
}
?>
];

var ds = new Ext.data.Store({
    reader: new Ext.data.ArrayReader({}, [
        {name: 'id'},
        {name: 'O_ID', type: 'int'},
        {name: 'COL1', type: 'string'},
        {name: 'COL2', type: 'string'},
        {name: 'COL3', type: 'string'},
        {name: 'COL7', type: 'string'},
        {name: 'COL4', type: 'string'},
        {name: 'COL5', type: 'string'},
        {name: 'COL6', type: 'string'}
    ])
});
ds.loadData(myData);
<?php if ($panel['name'] == 'OR') { ?>
     var colModel = new Ext.grid.ColumnModel([
         {header: "#", width: 40, sortable: true, locked:true, dataIndex: 'id'},
         {header: "Operacao.", width: 60, sortable: true, locked:true, dataIndex: 'O_ID'},
         {header: "Remessa", width: 70, sortable: true, locked:true, dataIndex: 'COL1'},
         {header: "Data", width: 120, sortable: true, locked:true, dataIndex: 'COL4'},
         {header: "Beneficiario", width: 300, sortable: true, locked:true,dataIndex: 'COL7'},
         {header: "Enviado", width: 100, sortable: true, align:'right', dataIndex: 'COL2'},
         {header: "Recebido", width: 100, sortable: true, align:'right', locked:true,dataIndex: 'COL3'},
         {header: "Taxa", width: 60, sortable: true, align:'right', locked:true,dataIndex: 'COL5'},
         {header: "Estado", width: 130, sortable: true, align:'right', locked:true,dataIndex: 'COL6'}
     ]);
<?php } elseif ($panel['name'] == 'CL') { ?>
     var colModel = new Ext.grid.ColumnModel([
         {header: "#", width: 40, sortable: true, locked:true, dataIndex: 'id'},
         {header: "Nm", width: 60, sortable: true, locked:true, dataIndex: 'O_ID'},
         {header: "Nome", width: 70, sortable: true, locked:true, dataIndex: 'COL5'},
         {header: "Telefone", width: 120, sortable: true, locked:true, dataIndex: 'COL1'},
         {header: "Telemovel", width: 200, sortable: true, dataIndex: 'COL2'},
         {header: "Morada", width: 200, sortable: true, locked:true,dataIndex: 'COL3'},
         {header: "Pais", width: 100, sortable: true, align:'right', locked:true,dataIndex: 'COL4'},
         {header: "Observacao", width: 130, sortable: true, align:'right', locked:true,dataIndex: 'COL6'}
     ]);
<?php } elseif ($panel['name'] == 'DC') { ?>
    var colModel = new Ext.grid.ColumnModel([
        {header: "78", width: 40, sortable: true, locked:true, dataIndex: 'id'},
        {header: "#", width: 60, sortable: true, locked:true, dataIndex: 'O_ID'},
        {header: "Tipo", width: 70, sortable: true, locked:true, dataIndex: 'COL5'},
        {header: "Numero", width: 120, sortable: true, locked:true, dataIndex: 'COL1'},
        {header: "Data", width: 120, sortable: true, dataIndex: 'COL2'},
        {header: "Emissor", width: 200, sortable: true, locked:true,dataIndex: 'COL3'},
        {header: "Funcionario", width: 100, sortable: true, align:'right', locked:true,dataIndex: 'COL4'},
        {header: "Estado", width: 130, sortable: true, align:'right', locked:true,dataIndex: 'COL6'}
    ]);
<?php } elseif ($panel['name'] == 'RC') { ?>
    var colModel = new Ext.grid.ColumnModel([
        {header: "#", width: 40, sortable: true, locked:true, dataIndex: 'id'},
        {header: "Nm.", width: 60, sortable: true, locked:true, dataIndex: 'O_ID'},
        {header: "Nome", width: 250, sortable: true, locked:true, dataIndex: 'COL1'},
        {header: "Telefone", width: 100, sortable: true, align:'right', locked:true, dataIndex: 'COL4'},
        {header: "Telemovel", width: 100, sortable: true, align:'right', dataIndex: 'COL2'},
        {header: "Pais", width: 100, sortable: true, locked:true,dataIndex: 'COL5'},
        {header: "Documento", width: 130, sortable: true, align:'right', locked:true,dataIndex: 'COL6'},
        {header: "Observacoes", width: 200, sortable: true, locked:true,dataIndex: 'COL3'}

    ]);
<?php } elseif ($panel['name'] == 'AC') { ?>
    var colModel = new Ext.grid.ColumnModel([
        {header: "#", width: 40, sortable: true, locked:true, dataIndex: 'id'},
        {header: "N. Registo", width: 60, sortable: true, locked:true, dataIndex: 'O_ID'},
        {header: "Agencia", width: 70, sortable: true, locked:true, dataIndex: 'COL2'},
        {header: "N. Conta", width: 120, sortable: true, locked:true, dataIndex: 'COL1'},
        {header: "Banco", width: 200, sortable: true, dataIndex: 'COL4'},
        {header: "Tipo", width: 100, sortable: true, locked:true,dataIndex: 'COL3'},
        {header: "Funcionario", width: 100, sortable: true, align:'right', locked:true,dataIndex: 'COL5'},
        {header: "Data de Registo", width: 130, sortable: true, align:'right', locked:true,dataIndex: 'COL6'}
    ]);
<?php } elseif ($panel['name'] == 'EX') { ?>
    var colModel = new Ext.grid.ColumnModel([
        {header: "#", width: 40, sortable: true, locked:true, dataIndex: 'id'},
        {header: "Nm.", width: 60, sortable: true, locked:true, dataIndex: 'O_ID'},
        {header: "Nome", width: 70, sortable: true, locked:true, dataIndex: 'COL5'},
        {header: "Telefone", width: 120, sortable: true, locked:true, dataIndex: 'COL1'},
        {header: "Telemovel", width: 200, sortable: true, dataIndex: 'COL2'},
        {header: "Morada", width: 200, sortable: true, locked:true,dataIndex: 'COL3'},
        {header: "Pais", width: 100, sortable: true, align:'right', locked:true,dataIndex: 'COL4'},
        {header: "Observacao", width: 130, sortable: true, align:'right', locked:true,dataIndex: 'COL6'}
    ]);
<?php } elseif ($panel['name'] == 'DP') { ?>
    var colModel = new Ext.grid.ColumnModel([
        {header: "#", width: 40, sortable: true, locked:true, dataIndex: 'id'},
        {header: "Nm.", width: 60, sortable: true, locked:true, dataIndex: 'O_ID'},
        {header: "Nome", width: 70, sortable: true, locked:true, dataIndex: 'COL5'},
        {header: "Telefone", width: 120, sortable: true, locked:true, dataIndex: 'COL1'},
        {header: "Telemovel", width: 200, sortable: true, dataIndex: 'COL2'},
        {header: "Morada", width: 200, sortable: true, locked:true,dataIndex: 'COL3'},
        {header: "Pais", width: 100, sortable: true, align:'right', locked:true,dataIndex: 'COL4'},
        {header: "Observacao", width: 130, sortable: true, align:'right', locked:true,dataIndex: 'COL6'}
    ]);
<?php } ?>
Bars = new Ext.Toolbar({

    store: this.store,
    height: 25,
    align: 'right',
    items : [ {
            text: "Ultimos 30 dias",
            handler: function(){
                if (Ext.getCmp('ActiveOb').getText() == 'CL') {
                    document.pp_frm.action='_response/call.php?get=Operative/view/rst_LS&OBJ=OR&Lim=1&SearchParam=FraCliNumero&SearchValue='+Ext.getCmp('ActiveObID').getText();
                    Ext.getCmp('qwinResults').close();
                    idtargetwindow('Remessas de '+Ext.getCmp('ActiveObName').getText()+' '+Ext.getCmp('ActiveObID').getText(),'qwinResults','Wntargett',800,260);
                    subform('pp_frm','Wntargett');}
            }
        },'-',{
            text: "Ultimo Trimestre",
            handler: function(){
                if (Ext.getCmp('ActiveOb').getText() == 'CL') {
                    document.pp_frm.action='_response/call.php?get=Operative/view/rst_LS&OBJ=OR&Lim=3&SearchParam=FraCliNumero&SearchValue='+Ext.getCmp('ActiveObID').getText();
                    Ext.getCmp('qwinResults').close();
                    idtargetwindow('Remessas de '+Ext.getCmp('ActiveObName').getText()+' '+Ext.getCmp('ActiveObID').getText(),'qwinResults','Wntargett',800,260);
                    subform('pp_frm','Wntargett');}
            }
        },'-',{
            text: "Ultimo Semestre",
            handler: function(){
                if (Ext.getCmp('ActiveOb').getText() == 'CL') {
                    document.pp_frm.action='_response/call.php?get=Operative/view/rst_LS&OBJ=OR&Lim=6&SearchParam=FraCliNumero&SearchValue='+Ext.getCmp('ActiveObID').getText();
                    Ext.getCmp('qwinResults').close();
                    idtargetwindow('Remessas de '+Ext.getCmp('ActiveObName').getText()+' '+Ext.getCmp('ActiveObID').getText(),'qwinResults','Wntargett',800,260);
                    subform('pp_frm','Wntargett');}
            }
        },'-',{
            text: "Ultimos 365 dias",
            handler: function(){
                if (Ext.getCmp('ActiveOb').getText() == 'CL') {
                    document.pp_frm.action='_response/call.php?get=Operative/view/rst_LS&OBJ=OR&Lim=12&SearchParam=FraCliNumero&SearchValue='+Ext.getCmp('ActiveObID').getText();
                    Ext.getCmp('qwinResults').close();
                    idtargetwindow('Remessas de '+Ext.getCmp('ActiveObName').getText()+' '+Ext.getCmp('ActiveObID').getText(),'qwinResults','Wntargett',800,260);
                    subform('pp_frm','Wntargett');}
            }
        },'-',{
            text: "Todos",
            handler: function(){
                if (Ext.getCmp('ActiveOb').getText() == 'CL') {
                    document.pp_frm.action='_response/call.php?get=Operative/view/rst_LS&OBJ=OR&Lim=0&SearchParam=FraCliNumero&SearchValue='+Ext.getCmp('ActiveObID').getText();
                    Ext.getCmp('qwinResults').close();
                    idtargetwindow('Remessas de '+Ext.getCmp('ActiveObName').getText()+' '+Ext.getCmp('ActiveObID').getText(),'qwinResults','Wntargett',800,260);
                    subform('pp_frm','Wntargett');}
            }
        },'-',' # <?php echo $CARD; ?> | EUR: <?php echo number_format($SUM, 2); ?>' ]
});

var selmode = new Ext.grid.RowSelectionModel({});
var Console = new Ext.grid.GridPanel({
    ds: ds,
    cm: colModel,
    sm: selmode,
    listeners: {
        rowdblclick: function(ds, row) {
            var record = ds.getStore().getAt(row);
            var value = record.get('O_ID');
            document.getElementById("ORPanel").innerHTML = "";
            if('<?php echo $panel['name']; ?>' != 'RC' && '<?php echo $panel['name']; ?>' != 'AC') {
                document.getElementById("CLPanel").innerHTML = "";
            }
            if('<?php echo $panel['name']; ?>' != 'AC') {
                document.getElementById("RCPanel").innerHTML = "";
            }
            document.getElementById("ACPanel").innerHTML = "";
            document.getElementById("EXPanel").innerHTML = "";
            document.getElementById("DPPanel").innerHTML = "";
<?php if ($panel['name'] == 'OR') { ?>
                 replaceUrl('<?php echo $panel['name']; ?>Panel','_response/call.php?get=Operative/frm_transferedit&<?php echo $panel['name']; ?>='+value);
<?php } else { ?>
                 replaceUrl('<?php echo $panel['name']; ?>Panel','_response/call.php?get=Operative/view/frm_<?php echo $panel['name']; ?>&<?php echo $panel['name']; ?>='+value);
<?php } ?>
            Ext.getCmp('ActiveObName').setText('<?php echo $OB_SELEC; ?>');
            Ext.getCmp('ActiveOb').setText('<?php echo $panel['name']; ?>');
            Ext.getCmp('ActiveObID').setText(value);
            Ext.getCmp('qwinResults').close();
        }
    },
    height: 232,
    bbar: Bars,
    renderTo: 'resultsList'
});
Ext.getCmp('win-statusbar').setStatus({text: 'Pronto.'});
<?php } else { ?>
    if(Ext.getCmp('src-window')) {
        Ext.getCmp('qwinResults').close();
        Ext.getCmp('win-statusbar').setStatus({text: 'Nao existem resultados para a consulta.'});
    }
<?php } ?>
});
</script>